# encoding:utf-8

import json
import os
import requests
import plugins
from bridge.context import ContextType
from bridge.reply import Reply, ReplyType
from common.log import logger
from plugins import *
from .SearchCNNVD import *


@plugins.register(
    name="漏洞情报",
    desire_priority=0,
    hidden=True,
    desc="漏洞搜索,支持CNNVD",
    version="0.1",
    author="XcNGG",
)
class VulnerabilityIntelligence(Plugin):
    def __init__(self):
        super().__init__()
        self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
        logger.info("[VulnerabilityIntelligence] inited.")

    def on_handle_context(self, e_context: EventContext):
        if e_context["context"].type != ContextType.TEXT:
            return
        content = e_context["context"].content.strip()
        # 判断匹配CNNVD的类型
        if content.startswith("CNNVD-"):
            search_cnnvd(number=content,e_context=e_context)

        # 判断匹配CVE的类型
        if content.startswith("CVE-"):
            search_cnnvd(number=content,e_context=e_context)


    def get_help_text(self, **kwargs):
        help_text = "-------------------------\n"
        help_text += "支持CNNVD查询，CVE查询\n"
        help_text += "快速查询：关键词【漏洞编号】\n"
        help_text += "例如: CNNVD-202311-2(注意大写)\n"
        help_text += "-------------------------\n"

        return help_text


def _send_info(e_context: EventContext, content: str):
    reply = Reply(ReplyType.TEXT, content)
    channel = e_context["channel"]
    channel.send(reply, e_context["context"])
